#include "include.h"
#include "func.h"
#include "func_bt.h"
#if FUNC_FCC_MODE_EN
u8 fcc_param[10] = { 0x00, 0x0A, 0x00, 0x00, 0x00, 0x01, 0x01, 0x2E, 0xA7, 0x02};
uint8_t *bt_rf_get_fcc_param(void)
{
    return fcc_param;
}
#endif // FUNC_FCC_MODE_EN


u8 key_hold_cnt=0;


void sfunc_adjust_ring_volume(u8 adjust_dir);
AT(.text.func.bt.msg)
void func_bt_message(u16 msg)
{

	if(msg!=0&&msg!=0x7ff&&(msg&0x800))
	{
		printf("key msg:0x%x\n",msg);
	}
    switch (msg) {
		case KU_HSF:
	    	{
	    		u8 light_mode;
				key_hold_cnt=0;
	    		light_mode=bsp_set_light_mode();
				bt_reset_light_idle_timeout();
	    	}
			break;
		case KL_HSF:
			bsp_set_pwm_mode();
			bt_reset_light_idle_timeout();
			break;
		case KH_HSF:
			if(key_hold_cnt>107)
			{
				break;
			}
			key_hold_cnt++;
			printf("hold cnt:%d\n",key_hold_cnt);
			if(key_hold_cnt%4==0)
			{
				bsp_set_pwm_mode();
				bt_reset_light_idle_timeout();
			}		
			break;
		case KLU_HSF:
			key_hold_cnt=0;
			break;
	case KH_PLAY_HSF:
		f_bt.idle_cnt=0;
		key_hold_cnt++;
		printf("hold cnt:%d\n",key_hold_cnt);
		if(key_hold_cnt==20)
		{
			func_play_sdk_version();
		}		
		break;
	case KLU_PLAY_HSF:
	case KU_PLAY_HSF:
		key_hold_cnt=0;
		break;
	case KL_POWER:
	//	bsp_set_pwm_mode();
	//	break;
	case KEY_POWER:
		
		if(long_wakeup)
		{
			long_wakeup=0;
			break;
		}
		#if 0
		{
			u8 light_mode;
    		light_mode=bsp_set_light_mode();
			break;
		}
		#endif
		f_bt.idle_cnt=0;
	    func_cb.sta=FUNC_IDLE;//PWROFF;
		//rgb_led2_reset();
		break;
	case KLU_POWER:
		long_wakeup=0;
		break;
	case KU_PLAY:
		if(f_bt.bt_status==1)
		{
			func_bt_cancel_pair_mode();
			break;
		}
		key_hold_cnt=0;
		f_bt.idle_cnt=0;
		bt_music_play_pause();
        f_bt.pp_2_unmute = sys_cb.mute;
		break;
	case KL_PLAY:
		printf("cur bt status:%d\n",f_bt.disp_status);
		bt_hfp_siri_switch();
						
		break;
	case KU_BT:
		key_hold_cnt=0;
		func_bt_cancel_pair_mode();
		break;
	case KH_BT:
		f_bt.idle_cnt=0;
		key_hold_cnt++;
		printf("hold cnt:%d\n",key_hold_cnt);
		if(key_hold_cnt==26)
		{
			bt_nor_delete_link_info();
			bsp_set_volume(SYS_INIT_VOLUME);
			//bsp_set_led_mode(LED_SEQUENCE_4);
			bsp_reset_light_mode();
			func_mp3_res_play(RES_BUF_EN_TIMEOUT_MP3, RES_LEN_EN_TIMEOUT_MP3);		
			cm_sync();
			//func_cb.sta=FUNC_PWROFF;
		}
		break;
	case KL_BT:
		if(long_wakeup)
		{
			break;
		}
		key_hold_cnt=0;
		func_bt_into_pair_mode();
		break;
	case KLU_BT:
		key_hold_cnt=0;
		break;
	
	case EVT_BT_RECONNECT:
		func_bt_reconnect_tts();
		break;
	case EVT_BT_RECONNECT_FAIL:
		//bsp_set_led_mode(LED_BT_PAIR_FAIL);
		func_bt_reconnect_fail_tts();
		break;
	case EVT_BT_LOSSCONNECT:
		func_bt_lossconnect_msg();
		break;
	case EVT_BT_RECONNECT_FINISH:
		//func_bt_reconnect_finish_msg();
		break;
    case KTH_PLAY:
    //case KU_PREV:
        bt_music_prev();
        sys_cb.key2unmute_cnt = 15 * sys_cb.mute;
        break;

    case KD_PLAY:
    //case KU_NEXT:
        bt_music_next();
        sys_cb.key2unmute_cnt = 15 * sys_cb.mute;
        break;
	case KEY_VOL_UP:
		if(sys_cb.sco_start)
		{
			sfunc_adjust_ring_volume(3);
		}
		else
		{
			func_bt_change_vol(3);
		}		
		break;
	case KL_VOL_UP:
	case KH_VOL_UP:
		if(sys_cb.sco_start)
		{
			sfunc_adjust_ring_volume(1);
		}
		else
		{
			func_bt_change_vol(1);
		}
		//func_bt_change_vol(1);
		break;
	case KEY_VOL_DOWN:
		if(sys_cb.sco_start)
		{
			sfunc_adjust_ring_volume(0);
		}
		else
		{
			func_bt_change_vol(0);
		}
		//func_bt_change_vol(0);
		break;
	case KL_VOL_DOWN:
	case KH_VOL_DOWN:
		if(sys_cb.sco_start)
		{
			sfunc_adjust_ring_volume(2);
		}
		else
		{
			func_bt_change_vol(2);
		}
		//func_bt_change_vol(2);
		break;
	#if 0
    case KU_PLAY:
    case KU_PLAY_HSF:
    case KU_PLAY_POWER:
    case KU_PLAY_MODE:
#if FUNC_FCC_MODE_EN
        if (func_cb.sta == FUNC_BT_FCC) {
//            printf("FCC_TEST\n");
            bt_fcc_test_start();
            break;
        }
#endif // FUNC_FCC_MODE_EN
        bt_music_play_pause();
        f_bt.pp_2_unmute = sys_cb.mute;
        break;

    case KU_PREV_VOL_DOWN:
    case KL_VOL_DOWN_PREV:
    case KU_PREV:
        bt_music_prev();
        sys_cb.key2unmute_cnt = 15 * sys_cb.mute;
        break;

    case KU_NEXT_VOL_UP:
    case KL_VOL_UP_NEXT:
    case KU_NEXT:
        bt_music_next();
        sys_cb.key2unmute_cnt = 15 * sys_cb.mute;
        break;
    case KL_PREV:
        bt_music_rewind();
        break;
    case KLU_PREV:
        bsp_clr_mute_sta();
        bt_music_rewind_end();
        break;
    case KL_NEXT:
        bt_music_fast_forward();
        break;
    case KLU_NEXT:
        bsp_clr_mute_sta();
        bt_music_fast_forward_end();
        break;
//    case KL_PLAY:
//        bsp_clr_mute_sta();
//        bt_disconnect(1);
//        break;

    case KU_HSF:
    case KD_PLAY:
	case KD_PLAY_HSF:
    case KD_PLAY_POWER:
    case KD_PLAY_MODE:
        bsp_clr_mute_sta();
        if (bt_get_status() >= BT_STA_CONNECTED) {
            bt_call_redial_last_number();           //回拨电话
#if WARNING_BT_CALLOUT
            func_mp3_res_play(RES_BUF_CALLOUT_MP3, RES_LEN_CALLOUT_MP3);
#endif
#if BT_TWS_EN
        } else if(xcfg_cb.bt_tws_en && xcfg_cb.bt_tws_pair_mode == 2) {
            if(bt_tws_is_connected()) {
                bt_tws_disconnect();
            } else {
                bt_tws_search_slave(15000);
            }
#endif
        } else {
            func_bt_switch_voice_lang();            //切换提示音语言
        }
        break;

	#endif
#if BT_TWS_EN
    case KL_MODE:
        if(xcfg_cb.bt_tws_en && xcfg_cb.bt_tws_pair_mode == 3) {
            if(bt_tws_is_connected()) {
                bt_tws_disconnect();
            } else {
                bt_tws_search_slave(15000);
            }
        }
        break;
#endif

    case KD_VOL_UP_NEXT:
    case KD_NEXT_VOL_UP:
        bsp_clr_mute_sta();
#if BT_HID_MENU_EN
        bsp_bt_hid_tog_conn();
#endif
        break;

    case KD_PREV_VOL_DOWN:
    case KD_VOL_DOWN_PREV:
        bsp_clr_mute_sta();
        bsp_bt_hid_photo();
        break;

    case MSG_SYS_1S:
		check_bt_ilde_timeout();
		#if 0
        bt_hfp_report_bat();
#if BT_CONNECTED_AUTO_PLAY_EN
        if (f_bt.autoplay > 0) {
            f_bt.autoplay++;
            if (f_bt.autoplay >= 3) {
                f_bt.autoplay = 0;
                if (bt_get_status() == BT_STA_CONNECTED) {
                    bt_music_play();
                    //printf("BT AutoPlay\n");
                }
            }
        }
#endif
		#endif
        break;
	case EVT_HFP_SET_VOL:
        bsp_change_volume(sys_cb.hfp_vol * sys_cb.hfp2sys_mul);
        break;
	case EVT_TWS_SET_VOL:
    case EVT_A2DP_SET_VOL:
		if(sys_cb.vol_sync_to_remote)
		{
			printf("wait sync to remote first!!\n");
			break;
		}
        if(bt_get_status() != BT_STA_INCALL) 
		{
            printf("A2DP SET VOL: %d\n", sys_cb.vol);
            bsp_change_volume(sys_cb.vol);
            gui_box_show_vol();
            param_sys_vol_write();
            sys_cb.cm_times = 0;
            sys_cb.cm_vol_change = 1;
        }
        break;
    case EVT_A2DP_MUSIC_PLAY:
		printf("fade in 11!!\n");
        dac_fade_in();
        if (f_bt.pp_2_unmute) {
            f_bt.pp_2_unmute = 0;
            bsp_clr_mute_sta();
        }
        break;

    case EVT_A2DP_MUSIC_STOP:
        dac_fade_out();
        break;
	case EVT_SCO_START:
		sys_cb.siri_mode=1;
		bsp_set_led_mode(LED_BT_CALL_COMING);
		func_sco_play_ctl(1);
		break;
	case EVT_SCO_END:
		func_sco_play_ctl(0);
		if(sys_cb.siri_mode)
		{
			bsp_set_led_mode(LED_POWER_ON);
		}
		sys_cb.siri_mode=0;
		break;
    case EVT_KEY_2_UNMUTE:
        bsp_clr_mute_sta();
        break;

#if BT_REC_EN
    case KU_REC:
        if ((!dev_is_online(DEV_SDCARD)) && (!dev_is_online(DEV_UDISK) && (!dev_is_online(DEV_SDCARD1)))) {
            break;
        }
        if (bt_is_connected()) {
            sfunc_record();
    #if REC_AUTO_PLAY
            if (rec_cb.flag_play) {
                rec_cb.flag_play = 0;
            #if SYS_KARAOK_EN
                u8 voice_bak = 0, func_sta = func_cb.sta;
                if (karaok_get_voice_rm_sta()) {
                    voice_bak = 1;
                    karaok_voice_rm_disable();
                }
                bsp_karaok_exit(AUDIO_PATH_KARAOK);
            #endif
                bt_audio_bypass();
                sfunc_record_play();
                bt_audio_enable();
                dac_fade_in();
            #if SYS_KARAOK_EN
                if (voice_bak) {
                    karaok_voice_rm_enable();
                }
                bsp_karaok_init(AUDIO_PATH_KARAOK, func_sta);
            #endif
            }
    #endif
        }
        break;
#endif // BT_REC_EN

    default:
        func_message(msg);
        break;
    }
}

AT(.text.func.btring.msg)
void sfunc_bt_ring_message(u16 msg)
{
	#if 1
	if(msg!=0&&msg!=0x7ff)
	{
		printf("key msg 00:0x%x\n",msg);
	}
	 switch (msg) {
    case KU_PLAY:	 //接听
        bsp_clr_mute_sta();
	 	bsp_set_led_mode(LED_BT_CONNECTED);
        bt_call_answer_incoming();
        break;

    case KL_PLAY:
		sys_cb.key_hangup=1;
		bsp_set_led_mode(LED_BT_CALL_REJECT);
        bsp_clr_mute_sta();
        bt_call_terminate();    //挂断
        break;
	case KU_VOL_UP:
		sfunc_adjust_ring_volume(3);
	break;
	case KL_VOL_UP:
	case KH_VOL_UP:
		sfunc_adjust_ring_volume(1);
		break;
	case KU_VOL_DOWN:
		sfunc_adjust_ring_volume(0);
		break;
	case KL_VOL_DOWN:
	case KH_VOL_DOWN:
		sfunc_adjust_ring_volume(2);
		break;
	case KU_POWER:
	case KL_POWER:
	    func_cb.sta=FUNC_PWROFF;
		break;
    case MSG_SYS_1S:
        bt_hfp_report_bat();
        break;

    default:
        func_message(msg);
        break;
    }
	#else
    switch (msg) {
    case KU_PLAY:
    case KU_HSF:                //接听
    case KU_PLAY_POWER:
    case KU_PLAY_MODE:
    case KU_PLAY_HSF:
        bsp_clr_mute_sta();
        bt_call_answer_incoming();
        break;

    case KL_PLAY:
    case KL_HSF:
    case KL_PLAY_HSF:
    case KL_PLAY_POWER:
    case KL_PLAY_MODE:
        bsp_clr_mute_sta();
        bt_call_terminate();    //挂断
        break;

    case MSG_SYS_1S:
        bt_hfp_report_bat();
        break;

    default:
        func_message(msg);
        break;
    }
	#endif
}

void sfunc_bt_call_message(u16 msg)
{
    u8 call_status;
	u8 siri_status;
	if(msg!=0&&msg!=0x7ff)
	{
		printf("key msg 11:0x%x\n",msg);
	}
    switch (msg) {
	#if 0
    case KU_PLAY:
    //case KU_HSF:
	//case KU_PLAY_HSF:
  //  case KU_PLAY_POWER:
   // case KU_PLAY_MODE:
        bsp_clr_mute_sta();
        call_status = bt_get_call_indicate();
        if(call_status == BT_CALL_INCOMING) {
            bt_call_answer_incoming();  //接听第2路通话
        } 
		#if 0
		else {
        	siri_status=bt_get_siri_status();
			printf("siri_status:%d\n",siri_status);
			if(siri_status)
			{
				bt_hfp_siri_switch();
			}
			else
			{
				bt_call_terminate();        //挂断当前通话
			}

            //func_mp3_res_play(RES_BUF_CALLOUT_MP3, RES_LEN_CALLOUT_MP3);
        }
		#endif
        break;
	#endif
    case KU_PLAY:
	case KL_PLAY:
		sys_cb.key_hangup=1;
        bsp_clr_mute_sta();
		siri_status=bt_get_siri_status();
		printf("siri_status:%d\n",siri_status);
		if(siri_status)
		{
			bt_hfp_siri_switch();
		}
		else
		{
			bt_call_terminate();        //挂断当前通话
		}
#if BT_HFP_CALL_PRIVATE_EN
        if (xcfg_cb.bt_hfp_private_en) {
            bt_call_private_switch();
            func_mp3_res_play(RES_BUF_PRIV_CALL_MP3, RES_LEN_PRIV_CALL_MP3);
        }
#endif // BT_HFP_CALL_PRIVATE_EN
        break;

    case KL_HSF:
		#if 0
        bsp_clr_mute_sta();
        call_status = bt_get_call_indicate();
        if(call_status == BT_CALL_INCOMING) {
            bt_call_terminate();        //拒接第2路通话
        } else if(call_status == BT_CALL_3WAY_CALL) {
            bt_call_swap();             //切换两路通话
        }
		#endif
        break;
	case KU_VOL_UP:
		sfunc_adjust_ring_volume(3);
	break;
	case KL_VOL_UP:
	case KH_VOL_UP:
		sfunc_adjust_ring_volume(1);
		break;
	case KU_VOL_DOWN:
		sfunc_adjust_ring_volume(0);
		break;
	case KL_VOL_DOWN:
	case KH_VOL_DOWN:
		sfunc_adjust_ring_volume(2);
		break;
	case KU_POWER:
	case KL_POWER:
	    func_cb.sta=FUNC_PWROFF;
		break;
	#if 0
    case KL_NEXT_VOL_UP:
    case KH_NEXT_VOL_UP:
    case KU_VOL_UP_NEXT:
    case KU_VOL_UP:
    case KL_VOL_UP:
    case KH_VOL_UP:
		sfunc_adjust_ring_volume(1);
		break;
        if (sys_cb.hfp_vol < 15) {
            sys_cb.hfp_vol++;
            bt_ctrl_msg(BT_CTL_HFP_SPK_GAIN);
            bsp_change_volume(sys_cb.hfp_vol * sys_cb.hfp2sys_mul);
        }
        gui_box_show_vol();
        break;

    case KL_PREV_VOL_DOWN:
    case KH_PREV_VOL_DOWN:
    case KU_VOL_DOWN_PREV:
    case KU_VOL_DOWN:
    case KL_VOL_DOWN:
    case KH_VOL_DOWN:
		sfunc_adjust_ring_volume(0);
		break;
        if (sys_cb.hfp_vol > 0) {
            sys_cb.hfp_vol--;
            bt_ctrl_msg(BT_CTL_HFP_SPK_GAIN);
            bsp_change_volume(sys_cb.hfp_vol * sys_cb.hfp2sys_mul);
        }
        gui_box_show_vol();
        break;
	#endif
#if BT_HFP_REC_EN
    case KU_REC:
        sfunc_record();
        break;
#endif

    case EVT_HFP_SET_VOL:
        bsp_change_volume(sys_cb.hfp_vol * sys_cb.hfp2sys_mul);
        gui_box_show_vol();
        break;

    case EVT_A2DP_MUSIC_PLAY:
		printf("fade in 00!!\n");
        dac_fade_in();
        break;

    case MSG_SYS_1S:
		bt_call_check_light_status();
        bt_hfp_report_bat();
        break;

    default:
        func_message(msg);
        break;
    }
}

#if FUNC_BTHID_EN
AT(.text.func.bt.msg)
void func_bthid_message(u16 msg)
{
    switch (msg) {
    case KU_PLAY:
    case KU_PLAY_POWER:
    case KU_PLAY_MODE:
        if (bt_get_status() < BT_STA_DISCONNECTING) {
            bt_connect(BT_POWER_UP_RECONNECT_TIMES);
        } else {
            bsp_bt_hid_photo();
            mp3_res_play(RES_BUF_TAKE_PHOTO_MP3,RES_LEN_TAKE_PHOTO_MP3);
        }
        break;

    case KL_PLAY:
        bt_disconnect(1);
        break;

    default:
        func_message(msg);
        break;
    }
}
#endif
